package com.example.api.controller;

import com.example.api.aspect.SystemControllerLog;
import com.example.api.model.entity.Commodity;
import com.example.api.model.enums.ActionTypeEnum;
import com.example.api.model.support.RespBean;
import com.example.api.service.CommodityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "商品管理")
@RestController
@RequestMapping("/api/commodity")
public class CommodityController {

    @Resource
    private CommodityService commodityService;

    @ApiOperation("添加商品")
    @PostMapping("/")
    @SystemControllerLog(description = "添加商品", actionType = ActionTypeEnum.CREATE)
    public RespBean save(@RequestBody Commodity commodity) {
        return commodityService.save(commodity);
    }

    @ApiOperation("删除商品")
    @DeleteMapping("/{id}")
    @SystemControllerLog(description = "删除商品", actionType = ActionTypeEnum.DELETE)
    public RespBean delete(@PathVariable Integer id) {
        return commodityService.delete(id);
    }

    @ApiOperation("更新商品")
    @PutMapping("/")
    @SystemControllerLog(description = "更新商品", actionType = ActionTypeEnum.UPDATE)
    public RespBean update(@RequestBody Commodity commodity) {
        return commodityService.update(commodity);
    }

    @ApiOperation("查询商品")
    @GetMapping("/")
    @SystemControllerLog(description = "查询商品", actionType = ActionTypeEnum.QUERY)
    public List<Commodity> findAll(String name) {
        return commodityService.findAll(name);
    }

    @ApiOperation("根据id查询商品")
    @GetMapping("/{id}")
    @SystemControllerLog(description = "根据id查询商品", actionType = ActionTypeEnum.QUERY)
    public Commodity findById(@PathVariable Integer id) {
        return commodityService.findById(id);
    }

    @ApiOperation("根据仓库id查询商品")
    @GetMapping("/warehouse/{id}")
    @SystemControllerLog(description = "根据仓库id查询商品", actionType = ActionTypeEnum.QUERY)
    public List<Commodity> findByWid(@PathVariable Integer id) {
        return commodityService.findByWid(id);
    }

}